<template>
  <div class="login">
    <div class="login-admin">
      <div class="login-admin-header">
        <h2>管理后台</h2>
        <p>官方出品的单页面后台管理模板系统</p>
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules"  auto-complete="on" label-position="left">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="Username" auto-complete="on">
            <svg-icon slot="prefix"  icon-class="user" />
            </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input v-model="loginForm.password" :type="passwordType" auto-complete="on">
            
            <svg-icon slot="prefix" icon-class="password" />

            <svg-icon slot="suffix" :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" @click="showPwd" />

            </el-input>
        </el-form-item>

        <el-form-item prop="verifyCode">
          <el-row>
            <el-col :span="16">
                <el-input  v-model="loginForm.verifyCode"  placeholder="验证码" auto-complete="on">
                  <i slot="prefix"  class="el-icon-picture-outline" />
                </el-input>
            </el-col>
            <el-col :span="8">
              <el-image style="height:40px;" :src="verifyCodeImg" @click="randVerifyCode" ></el-image>
            </el-col>
          </el-row>
          
           
        </el-form-item>

        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin"  @keyup.enter.native="handleLogin" >登 录</el-button>

       
      </el-form>
    </div>
  </div>
</template>

<script>
import { validatePhone } from '@/utils/validate'
import  {verify} from '@/api/user';

export default {
  name: 'Login',
  data() {
    const validatePhones = (rule, value, callback) => {
      if (!validatePhone(value)) {
        callback(new Error("请填写正确的手机号"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: '',
        verifyCode:''
      },
      loginRules: {
        username: [{required: true,message: "请输入用户名", trigger: ["blur", "change"] }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        verifyCode: [ { required: true, message: "请输入验证码", trigger: ["blur" ,"change"] } ],
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      verifyCodeImg:''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted() {
      this.getVerify();
  },
  methods: {
    randVerifyCode(){
      this.getVerify();
    },
    async getVerify()  {
        const res = await verify();
        if(res.code == 200) {
            this.verifyCodeImg = res.data.verifyCodeImg;
        }
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
        
          this.$store.dispatch("user/login", this.loginForm).then(res => {
            if(res.code == 200) {
              this.$router.push({ path: this.redirect || '/' })
            }
            
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
.login{
      position: relative;
    left: 0;
    top: 0;
    padding: 110px 0;
    min-height: 100%;
    box-sizing: border-box;
}
.login-admin{
    width: 22rem;
    margin: 0 auto;
    box-sizing: border-box;
}

.login-admin-header{
  text-align: center;
}

.register-link{
  float:right;
}

</style>
